package com.gjintel.workflow.delegate;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.JavaDelegate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.gjintel.business.proj.model.ProjectInfo;
import com.gjintel.business.proj.service.IProjectInfoService;
import com.gjintel.business.proj.service.impl.ProjectInfoServiceImpl;
import com.gjintel.framework.constant.Constant;
import com.gjintel.framework.constant.ProjectStatus;
import com.gjintel.framework.dataset.model.DataSet;
import com.gjintel.framework.dataset.service.IDataSetService;
import com.gjintel.framework.dataset.service.impl.DataSetServiceImpl;
import com.gjintel.framework.holidayset.service.IHolidaySetService;
import com.gjintel.sdk.util.DateUtils;
import com.gjintel.sdk.util.StringUtil;

/**
 * @description 系统评标
 * @author wwang 
 * @date 2016-8-24
 */
public class EvaluatDelegate implements JavaDelegate{

	@Autowired
	private IProjectInfoService projectInfoService;
	
	@Autowired
	private IDataSetService dataSetService;
	
	@Autowired
	private IHolidaySetService holidaySetService;
	
	@Override
	public void execute(DelegateExecution execution) throws Exception {
		ApplicationContext ac = new  ClassPathXmlApplicationContext("classpath*:applicationContext.xml"); 
		projectInfoService=(ProjectInfoServiceImpl)ac.getBean("projectInfoServiceImpl");
		dataSetService=(DataSetServiceImpl)ac.getBean("dataSetService");
		
		//Thread.sleep(5000);
		String bkey=execution.getProcessBusinessKey();
		String tendeeerId=projectInfoService.saveTendeeByProjId(bkey);     //中标用户id
		List<String> rec = new ArrayList<String>();
		rec.add(tendeeerId);//中标人
		String buyer1=(String)execution.getVariable("applyUserId");
		rec.add(buyer1);//采购方
		execution.setVariable("receivers", rec);
		
		//设置公示 时间
		//获取系统设置的中标公示时间
		DataSet dataset = dataSetService.get(DataSet.class, Constant.DATASET_ID);
		int publicity= dataset.getPublicity();//公示天数
//		String publicityDate = StringUtil.computeDate(new Date(), "00:00:00", publicity+1);//公示时间
		String publicityDate = DateUtils.computeWorkDayDateStr(holidaySetService,new Date(), publicity);//(工作日)公示时间
		execution.setVariable("timeHit", StringUtil.getISODate(publicityDate));
		
		//设置项目状态
		ProjectInfo project = projectInfoService.get(ProjectInfo.class, bkey);
		project.setStatus(Integer.valueOf(ProjectStatus.SHOWING.toString()));//中标公示中
		projectInfoService.saveOrUpdate(project);
		
	}

}
